<template>
  <div>
    <el-card class="box-card">
      <div
        slot="header"
        class="clearfix"
      >
        <!-- <el-button >全部</el-button>
       <el-button>收藏</el-button> -->
        <el-radio-group
          v-model="reqParams.collect"
          size="mini"
          @change='collectChange'
        >
          <el-radio-button :label='false'>全部</el-radio-button>
          <el-radio-button :label="true">收藏</el-radio-button>
        </el-radio-group>
        <el-button
          style="float: right; padding: 3px 0"
          type="text"
        >添加素材</el-button>
      </div>
      <div class="imag_list">
        <div
          class="imag_item"
          v-for="item in imgData"
          :key="item.id"
        >
          <img
            :src="item.url"
            alt=""
          >
          <div class="icon_box">
            <span
              @click='off(item)'
              class='el-icon-star-off '
              :class="{red:item.is_collected }"
            ></span>
            <span
              @click='delImage(item.id)'
              class='el-icon-delete'
            ></span>
          </div>

        </div>

      </div>
      <el-pagination
        background
        layout="prev, pager, next"
        @current-change='change_page'
        :page-size='Number(reqParams.per_page)'
        :current-page='Number(reqParams.page)'
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "images",

  data() {
    return {
      collect: false,
      reqParams: {
        collect: false,
        page: 1,
        per_page: 10
      },
      total: 0,
      imgData: []
    };
  },

  created() {
    this.getImages();
  },

  methods: {
    collectChange(v) {
      console.log(v);
      // this.reqParams.collect = v
      this.reqParams.page = 1;
      this.getImages();
    },

    // 获取图片
    async getImages() {
      const {
        data: { data }
      } = await this.$http.get("/user/images", {
        params: this.reqParams
      });
      // console.log("图片", res);
      this.imgData = data.results;
      this.total = data.total_count;
    },

    // 收藏图片
    async off(item) {
      console.log(item);
      try {
        const res = await this.$http.put(`/user/images/${item.id}`, {
          collect: !item.is_collected
        });
        this.$message.success(res.data.data.collect ? "收藏成功" : "取消收藏");
        this.getImages();
      } catch (e) {
        this.$message({
          message: "收藏失败",
          type: "warning"
        });
      }
    },

    // 删除图片
    delImage(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            const res = await this.$http.delete(`user/images/${id}`);
            console.log("删除", res);
            this.$message.success("删除成功");
            this.getImages();
          } catch (e) {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    // 分页
    change_page(v) {
      console.log(v);
      this.reqParams.page = Number(v);
      this.getImages();
    }
  }
};
</script>

<style lang="less" scoped>
.imag_list {
  margin-bottom: 15px;
  .imag_item {
    width: 150px;
    height: 140px;
    border: 1px dashed #ddd;
    display: inline-block;
    margin-right: 15px;
    margin-top: 5px;
    position: relative;
    img {
      width: 100%;
      height: 100%;
      display: block;
    }
    .icon_box {
      width: 100%;
      height: 30px;
      background: rgba(0, 0, 0, 0.3);
      text-align: center;
      line-height: 30px;
      position: absolute;
      left: 0;
      bottom: 0;
      color: papayawhip;
      span {
        margin: 0 15px;
        font-size: 14px;
        &.red {
          color: red;
        }
      }
    }
  }
}
</style>>